package com.itdct.onflow.system.dict.listener

import com.itdct.onflow.core.extend.logError
import com.itdct.onflow.system.dict.global.DictLock
import com.itdct.onflow.system.dict.manager.SysDictManager
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
import org.springframework.data.redis.connection.Message
import org.springframework.data.redis.connection.MessageListener
import org.springframework.stereotype.Component

/**
 * @author Zhouwx
 * @date 2025/9/5 10:29:52
 * @version 1.0
 * @description
 */
@ConditionalOnProperty(name = ["onflow.dict.enable"], havingValue = "true")
@Component
class SysDictRefreshListener(
    val sysDictManager: SysDictManager
) : MessageListener {
    override fun onMessage(message: Message, pattern: ByteArray?) {
        try {
            DictLock.lock.lock()
            sysDictManager.initDict()
        } catch (e: Exception) {
            logError(e)
        } finally {
            DictLock.lock.unlock()
        }
    }
}